package router

import (
	"admin-system-go/model"
	"admin-system-go/model/dto"
	"admin-system-go/service"
	"github.com/gin-gonic/gin"
	"net/http"
)

type PermissionRouter struct {
	permissionService service.PermissionService
}

func NewPermissionRouter() PermissionRouter {
	return PermissionRouter{permissionService: service.GetPermissionService()}
}

func (permissionRouter PermissionRouter) Register(r *gin.Engine) {
	r.GET(apiVersion+"/user/permission/list", permissionRouter.QueryPermission())
	r.POST(apiVersion+"/user/permission/create", permissionRouter.createPermission())
	r.POST(apiVersion+"/user/permission/delete", permissionRouter.deletePermission())
	r.POST(apiVersion+"/user/permission/update", permissionRouter.updatePermission())
}

func (permissionRouter PermissionRouter) QueryPermission() gin.HandlerFunc {
	return func(context *gin.Context) {
		var permissionQueryDTO dto.PermissionQueryDTO
		err := context.ShouldBind(&permissionQueryDTO)
		if err != nil {
			context.JSON(http.StatusOK, model.ParamErrorModel)
			return
		}
		context.JSON(http.StatusOK, permissionRouter.permissionService.QueryPermission(permissionQueryDTO))
	}
}

func (permissionRouter PermissionRouter) createPermission() gin.HandlerFunc {
	return func(context *gin.Context) {
		var permissionBaseDTO dto.PermissionBaseDTO
		err := context.ShouldBind(&permissionBaseDTO)
		if err != nil {
			context.JSON(http.StatusOK, model.ParamErrorModel)
			return
		}
		context.JSON(http.StatusOK, permissionRouter.permissionService.CreatePermission(permissionBaseDTO))
	}
}

func (permissionRouter PermissionRouter) deletePermission() gin.HandlerFunc {
	return func(context *gin.Context) {
		var permissionDelDTO dto.PermissionDelDTO
		err := context.ShouldBind(&permissionDelDTO)
		if err != nil {
			context.JSON(http.StatusOK, model.ParamErrorModel)
			return
		}
		context.JSON(http.StatusOK, permissionRouter.permissionService.DelPermission(permissionDelDTO))
	}
}

func (permissionRouter PermissionRouter) updatePermission() gin.HandlerFunc {
	return func(context *gin.Context) {
		var permissionUpdateDTO dto.PermissionUpdateDTO
		err := context.ShouldBind(&permissionUpdateDTO)
		if err != nil {
			context.JSON(http.StatusOK, model.ParamErrorModel)
			return
		}
		context.JSON(http.StatusOK, permissionRouter.permissionService.UpdatePermission(permissionUpdateDTO))
	}
}
